<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入主题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="集合地点" prop="startAddress">
        <el-input
          v-model="queryParams.startAddress"
          placeholder="请输入集合地点"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="集合地点名称" prop="startName">
        <el-input
          v-model="queryParams.startName"
          placeholder="请输入集合地点名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>





      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in top"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['operate:cycling:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['operate:cycling:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['operate:cycling:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['operate:cycling:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" :columns="columns" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cyclingList" @selection-change="handleSelectionChange" @sort-change="changeTableSort">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="发起人" align="center" sortable="custom" v-if="columns[0].visible" prop="name" />
      <el-table-column label="主题" align="center" sortable="custom" v-if="columns[1].visible" prop="title" />
      <el-table-column label="集合地点" align="center" sortable="custom" v-if="columns[2].visible" prop="startAddress" />
      <el-table-column label="集合地点名称" align="center" sortable="custom" v-if="columns[3].visible" prop="startName" />
      <el-table-column label="目的地地点" align="center" sortable="custom" v-if="columns[4].visible" prop="endAddress" />
      <el-table-column label="目的地名称" align="center" sortable="custom" v-if="columns[5].visible" prop="endName" />
      <el-table-column label="开始时间" align="center" sortable="custom" v-if="columns[6].visible" prop="startTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" sortable="custom" v-if="columns[7].visible" prop="endTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动人数" align="center" sortable="custom" v-if="columns[8].visible" prop="applyNum" />
      <el-table-column label="联系方式" align="center" sortable="custom" v-if="columns[9].visible" prop="contact" />
      <el-table-column label="封面图片" align="center" v-if="columns[10].visible" prop="cover">
        <template #default="scope">
          <image-preview :src="scope.row.cover" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="热度" align="center" sortable="custom" v-if="columns[11].visible" prop="hot" />
      <el-table-column label="浏览次数" align="center" sortable="custom" v-if="columns[12].visible" prop="count" />
      <el-table-column label="距离" align="center" sortable="custom" v-if="columns[13].visible" prop="distance" />
      <el-table-column label="权重" align="center" sortable="custom" v-if="columns[14].visible" prop="weigh" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="1"
            inactive-value="0"
            @change="changeSwitchStatus($event,scope.row)"
          />
          <!-- <dict-tag :options="top" :value="scope.row.status"/> -->
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['operate:cycling:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['operate:cycling:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户出游管理对话框 -->
    <el-dialog :title="title" v-model="open" width="50vw" append-to-body :close-on-click-modal="false">
      <el-form ref="cyclingRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="发起人" prop="userId">
          <inputsearch :changeValue="changeUserValue" :inputName="'userId'" :inputValue="form.userId" :list="showUserList" />
        </el-form-item>
        <el-form-item label="主题" prop="title">
          <el-input v-model="form.title" placeholder="请输入主题" />
        </el-form-item>
        <el-form-item label="集合地点" prop="startAddress">
          <el-input v-model="form.startAddress" placeholder="请输入集合地点" />
        </el-form-item>
        <el-form-item label="集合地点名称" prop="startName">
          <el-input v-model="form.startName" placeholder="请输入集合地点名称" />
        </el-form-item>
        <el-form-item label="目的地地点" prop="endAddress">
          <el-input v-model="form.endAddress" placeholder="请输入目的地地点" />
        </el-form-item>
        <el-form-item label="目的地名称" prop="endName">
          <el-input v-model="form.endName" placeholder="请输入目的地名称" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动人数" prop="applyNum">
          <el-input v-model="form.applyNum" type="number" placeholder="请输入活动人数" />
        </el-form-item>
        <el-form-item label="联系方式" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="活动描述" prop="desc">
          <editor v-model="form.desc" :min-height="192"/>
          <!-- <el-input v-model="form.desc" type="textarea" placeholder="请输入内容" /> -->
        </el-form-item>
        <el-form-item label="封面图片" prop="cover">
          <el-input v-model="form.cover" placeholder="请输入封面图片" />
        </el-form-item>
        <el-form-item label="开始纬度" prop="startLatitude">
          <el-input v-model="form.startLatitude" placeholder="请输入开始纬度" />
        </el-form-item>
        <el-form-item label="开始经度" prop="startLongitude">
          <el-input v-model="form.startLongitude" placeholder="请输入开始经度" />
        </el-form-item>
        <el-form-item label="结束纬度" prop="endLatitude">
          <el-input v-model="form.endLatitude" placeholder="请输入结束纬度" />
        </el-form-item>
        <el-form-item label="结束经度" prop="endLongitude">
          <el-input v-model="form.endLongitude" placeholder="请输入结束经度" />
        </el-form-item>
        <el-form-item label="热度" prop="hot">
          <el-input v-model="form.hot" type="number" placeholder="请输入热度" />
        </el-form-item>
        <el-form-item label="浏览次数" prop="count">
          <el-input v-model="form.count" type="number" placeholder="请输入浏览次数" />
        </el-form-item>
        <el-form-item label="距离" prop="distance">
          <el-input v-model="form.distance" type="number" placeholder="请输入距离" />
        </el-form-item>
        <el-form-item label="权重" prop="weigh">
          <el-input v-model="form.weigh" type="number" placeholder="请输入权重" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in top"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Cycling">
import { listCycling, getCycling, delCycling, addCycling, updateCycling } from "@/api/operate/cycling";
import { listsUser } from "@/api/userInfo/group";
import inputsearch from "@/components/inputsearch";

const { proxy } = getCurrentInstance();
const { top } = proxy.useDict('top');

const columns = ref([
  { key: 0, label: `发起人`,prop: 'name', visible: true },
  { key: 1, label: `主题`,prop: 'title', visible: true },
  { key: 2, label: `集合地点`,prop: 'startAddress', visible: true },
  { key: 3, label: `集合地点名称`,prop: 'startName', visible: true },
  { key: 4, label: `目的地地点`,prop: 'endAddress', visible: true },
  { key: 5, label: `目的地名称`,prop: 'endName', visible: true },
  { key: 6, label: `开始时间`,prop: 'startTime', visible: true },
  { key: 7, label: `结束时间`,prop: 'endTime', visible: true },
  { key: 8, label: `活动人数`,prop: 'applyNum', visible: true },
  { key: 9, label: `联系方式`,prop: 'contact', visible: true },
  { key: 10, label: `封面图片`,prop: 'cover', visible: true },
  { key: 11, label: `热度`,prop: 'hot', visible: true },
  { key: 12, label: `浏览次数`,prop: 'count', visible: true },
  { key: 13, label: `距离`,prop: 'distance', visible: true },
  { key: 14, label: `权重`,prop: 'weigh', visible: true }
]);
const cyclingList = ref([]);
const showUserList = ref([]);                         // 用户列表
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: '',
    title: null,
    startAddress: null,
    startName: null,
    endAddress: null,
    endName: null,
    startTime: null,
    endTime: null,
    applyNum: null,
    contact: null,
    desc: null,
    cover: null,
    startLatitude: null,
    startLongitude: null,
    endLatitude: null,
    endLongitude: null,
    hot: null,
    count: null,
    distance: null,
    top: null,
    weigh: null,
    status: null,
    orderByColumn: null,
    isAsc: null
  },
  rules: {
    userId: [
      { required: true, message: "发起人不能为空", trigger: "blur" }
    ],
    title: [
      { required: true, message: "主题不能为空", trigger: "blur" }
    ],
    startAddress: [
      { required: true, message: "集合地点不能为空", trigger: "blur" }
    ],
    startName: [
      { required: true, message: "集合地点名称不能为空", trigger: "blur" }
    ],
    endAddress: [
      { required: true, message: "目的地地点不能为空", trigger: "blur" }
    ],
    endName: [
      { required: true, message: "目的地名称不能为空", trigger: "blur" }
    ],
    applyNum: [
      { required: true, message: "活动人数不能为空", trigger: "blur" }
    ],
    contact: [
      { required: true, message: "联系方式不能为空", trigger: "blur" }
    ],
    cover: [
      { required: true, message: "封面图片不能为空", trigger: "blur" }
    ],
    startLatitude: [
      { required: true, message: "开始纬度不能为空", trigger: "blur" }
    ],
    startLongitude: [
      { required: true, message: "开始经度不能为空", trigger: "blur" }
    ],
    endLatitude: [
      { required: true, message: "结束纬度不能为空", trigger: "blur" }
    ],
    endLongitude: [
      { required: true, message: "结束经度不能为空", trigger: "blur" }
    ],
    hot: [
      { required: true, message: "热度不能为空", trigger: "blur" }
    ],
    count: [
      { required: true, message: "浏览次数不能为空", trigger: "blur" }
    ],
    distance: [
      { required: true, message: "距离不能为空", trigger: "blur" }
    ],
    top: [
      { required: true, message: "置顶:0=否,1=是不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态:0=待审核,1=已通过不能为空", trigger: "change" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询用户出游管理列表 */
function getList() {
  loading.value = true;
  listCycling(queryParams.value).then(response => {
    cyclingList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查询会员列表 */
function getUserList() {
  listsUser({
    pageNum: 1,
    pageSize: 10000
  }).then(response => {
    showUserList.value = response.rows.map(e => {
      e.value = e.username+(e.name ? '('+e.name+')' : '')
      return e
    });
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: '',
    title: null,
    startAddress: null,
    startName: null,
    endAddress: null,
    endName: null,
    startTime: null,
    endTime: null,
    applyNum: null,
    contact: null,
    desc: '',
    cover: null,
    startLatitude: null,
    startLongitude: null,
    endLatitude: null,
    endLongitude: null,
    hot: null,
    count: null,
    distance: null,
    top: null,
    weigh: null,
    createTime: null,
    updateTime: null,
    status: null
  };
  proxy.resetForm("cyclingRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value.orderByColumn = null
  queryParams.value.isAsc = null
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加用户出游管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getCycling(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改用户出游管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["cyclingRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCycling(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCycling(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

// 修改状态
function changeSwitchStatus(e,row) {
  updateCycling(row).then(response => {
    proxy.$modal.msgSuccess("修改成功");
    // getList();
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除用户出游管理编号为"' + _ids + '"的数据项？').then(function() {
    return delCycling(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('operate/cycling/export', {
    ...queryParams.value
  }, `cycling_${new Date().getTime()}.xlsx`)
}

// 改变用户选择值
const changeUserValue = (request) => {
  form.value.userId = request.userId
}

// 改变排序
function changeTableSort(e){
  if(e.order){
    queryParams.value.orderByColumn = e.prop
    queryParams.value.isAsc = e.order == 'descending' ? 'desc' : 'asc'
  }else{
    queryParams.value.orderByColumn = null
    queryParams.value.isAsc = null
  }
  handleQuery()
}

getList();
getUserList();
</script>
